"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SpriteDataAtlas = exports.SpriteData = exports.SpriteDataFormat = void 0;
const transfer_js_1 = require("../io/transfer.js");
var SpriteDataFormat;
(function (SpriteDataFormat) {
    SpriteDataFormat[SpriteDataFormat["Bit1AndAlpha"] = 0] = "Bit1AndAlpha";
    SpriteDataFormat[SpriteDataFormat["Bit8"] = 1] = "Bit8";
    SpriteDataFormat[SpriteDataFormat["RGBA"] = 2] = "RGBA";
})(SpriteDataFormat || (exports.SpriteDataFormat = SpriteDataFormat = {}));
class SpriteData {
    constructor() {
        this.index = 0;
        //中心点
        this.pivotX = 0;
        this.pivotY = 0;
        //尺寸
        this.sizeX = 0;
        this.sizeY = 0;
        //format
        this.format = SpriteDataFormat.RGBA;
        //pixels
        this.data = []; //
    }
    //得到一个像素的容量
    GetPixelSize() {
        if (this.format == SpriteDataFormat.RGBA)
            return 4;
        else
            return 1;
    }
    //得到Data的容量
    GetDataSize() {
        if (this.format == SpriteDataFormat.RGBA)
            return this.sizeX * this.sizeY * 4;
        else
            return this.sizeX * this.sizeY;
    }
    ReadMutli(count, func) {
        let arr = [];
        for (let i = 0; i < count; i++)
            func();
        return arr;
    }
    WriteMutli(count, data, func) {
        let arr = [];
        for (let i = 0; i < count; i++)
            func(data[i]);
        return arr;
    }
    Transfer(stream, mode) {
        if (mode == transfer_js_1.TransferMode.Read) {
            this.index = stream.ReadU16();
            this.sizeX = stream.ReadDynUInt();
            this.sizeY = stream.ReadDynUInt();
            this.pivotX = stream.ReadDynUInt();
            this.pivotY = stream.ReadDynUInt();
            this.format = stream.ReadByte();
            //玩个花活
            let readfunc = (this.GetPixelSize() == 1 ? stream.ReadByte : stream.ReadU32).bind(stream);
            this.data = this.ReadMutli(this.sizeX * this.sizeY, readfunc);
        }
        else {
            stream.WriteU16(this.index);
            stream.WriteDynUInt(this.sizeX);
            stream.WriteDynUInt(this.sizeY);
            stream.WriteDynUInt(this.pivotX);
            stream.WriteDynUInt(this.pivotY);
            stream.WriteByte(this.format);
            let writefunc = (this.GetPixelSize() == 1 ? stream.WriteByte : stream.WriteU32).bind(stream);
            this.WriteMutli(this.sizeX * this.sizeY, this.data, writefunc);
        }
    }
}
exports.SpriteData = SpriteData;
//Sprite 数据集
class SpriteDataAtlas {
    constructor() {
        this.sprites = [];
        this.spriteNameMap = {};
    }
    Transfer(stream, mode) {
        //transfer Head
        let spriteNum = 0;
        let NameNum = 0;
        let keys = [];
        if (mode == transfer_js_1.TransferMode.Read) {
            let magichead = stream.ReadStr();
            if (magichead != "atlas")
                throw "error head.";
            spriteNum = stream.ReadDynUInt();
            NameNum = stream.ReadDynUInt();
        }
        else {
            let magichead = "atlas";
            stream.WriteStr(magichead);
            spriteNum = this.sprites.length;
            for (let key in this.spriteNameMap) {
                keys.push(key);
            }
            NameNum = keys.length;
            stream.WriteDynUInt(spriteNum);
            stream.WriteDynUInt(NameNum);
        }
        //transfer Sprite
        if (mode == transfer_js_1.TransferMode.Read) {
            this.sprites = [];
            for (var i = 0; i < spriteNum; i++) {
                this.sprites.push(new SpriteData());
            }
        }
        for (var i = 0; i < spriteNum; i++) {
            this.sprites[i].Transfer(stream, mode);
        }
        //transfer NamedInfo
        if (mode == transfer_js_1.TransferMode.Read) {
            this.spriteNameMap = {};
            for (var i = 0; i < NameNum; i++) {
                let key = stream.ReadStr();
                let value = stream.ReadU16();
                this.spriteNameMap[key] = value;
            }
        }
        else {
            for (var i = 0; i < NameNum; i++) {
                let key = keys[i];
                let value = this.spriteNameMap[key];
                stream.WriteStr(key);
                stream.WriteU16(value);
            }
        }
    }
    GetSpriteByIndex(index) {
        return this.sprites[index];
    }
    GetSpriteIndex(name) {
        let index = this.spriteNameMap[name];
        if (index == undefined)
            return -1;
        return index;
    }
}
exports.SpriteDataAtlas = SpriteDataAtlas;
